package com.ruoyi.jiugongge.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.jiugongge.mapper.JiugonggePrizesMapper;
import com.ruoyi.jiugongge.mapper.JiugonggeRecordMapper;
import com.ruoyi.jiugongge.mapper.JiugonggeRegistrationMapper;
import com.ruoyi.jiugongge.mapper.JiugonggeRelationMapper;
import com.ruoyi.jiugongge.domain.JiugonggePrizes;
import com.ruoyi.jiugongge.domain.JiugonggeRecord;
import com.ruoyi.jiugongge.domain.JiugonggeRegistration;
import com.ruoyi.jiugongge.service.IJiugonggeRecordService;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.StringUtils;

/**
 * 九宫格抽奖记录 服务层实现
 * 
 * @author lzy
 * @date 2019-02-20
 */
@Service
public class JiugonggeRecordServiceImpl implements IJiugonggeRecordService 
{
	@Autowired
	private JiugonggeRecordMapper jiugonggeRecordMapper;
	@Autowired
	private JiugonggeRegistrationMapper jiugonggeRegistrationMapper;
	@Autowired
	private JiugonggeRelationMapper jiugonggeRelationMapper;
	@Autowired
	private JiugonggePrizesMapper jiugonggePrizesMapper;

	/**
     * 查询九宫格抽奖记录信息
     * 
     * @param id 九宫格抽奖记录ID
     * @return 九宫格抽奖记录信息
     */
    @Override
	public JiugonggeRecord selectJiugonggeRecordById(String id)
	{
	    return jiugonggeRecordMapper.selectJiugonggeRecordById(id);
	}
	
	/**
     * 查询九宫格抽奖记录列表
     * 
     * @param jiugonggeRecord 九宫格抽奖记录信息
     * @return 九宫格抽奖记录集合
     */
	@Override
	public List<JiugonggeRecord> selectJiugonggeRecordList(JiugonggeRecord jiugonggeRecord)
	{
	    return jiugonggeRecordMapper.selectJiugonggeRecordList(jiugonggeRecord);
	}
	
    /**
     * 新增九宫格抽奖记录
     * 
     * @param jiugonggeRecord 九宫格抽奖记录信息
     * @return 结果
     */
	@Override
	public int insertJiugonggeRecord(JiugonggeRecord jiugonggeRecord)
	{
	    return jiugonggeRecordMapper.insertJiugonggeRecord(jiugonggeRecord);
	}
	
	/**
     * 修改九宫格抽奖记录
     * 
     * @param jiugonggeRecord 九宫格抽奖记录信息
     * @return 结果
     */
	@Override
	public int updateJiugonggeRecord(JiugonggeRecord jiugonggeRecord)
	{
	    return jiugonggeRecordMapper.updateJiugonggeRecord(jiugonggeRecord);
	}

	/**
     * 删除九宫格抽奖记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteJiugonggeRecordByIds(String ids)
	{
		return jiugonggeRecordMapper.deleteJiugonggeRecordByIds(Convert.toStrArray(ids));
	}

	@Override
	public int getCountByActId(String actId) {
		// TODO Auto-generated method stub
		return jiugonggeRecordMapper.getCountByActId(actId);
	}

	@Override
	public List<JiugonggeRecord> queryRecordListByOpenidAndActidAndJwid(String openid, String actId, String jwid,
			String currDate) {
		// TODO Auto-generated method stub
		return jiugonggeRecordMapper.queryBargainRecordListByOpenidAndActidAndJwidAndCurdate(openid,actId,jwid,currDate);
	}
	
	@Override
	public List<JiugonggeRecord> queryRecordListByOpenidAndActidAndJwid(String openid, String actId, String jwid) {
		// TODO Auto-generated method stub
		return jiugonggeRecordMapper.queryBargainRecordListByOpenidAndActidAndJwid(openid,actId, jwid);
	}

	@Override
	public JiugonggeRecord queryByActIdAndawardCode(String actId, String awardCode) {
		// TODO Auto-generated method stub
		return jiugonggeRecordMapper.queryByActIdAndawardCode(actId,awardCode);
	}

	@Override
	public List<JiugonggeRecord> queryMyAwardsByOpenidAndActidAndJwid(String openid, String actId, String jwid) {
		// TODO Auto-generated method stub
		return jiugonggeRecordMapper.queryMyAwardsByOpenidAndActidAndJwid(openid,actId,jwid);
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public JiugonggePrizes creatAwards(JiugonggeRecord wxActJiugonggeRecord) {
		// TODO Auto-generated method stub
		//获取中奖序号
		Integer maxAwardsSeq = jiugonggeRecordMapper.getMaxAwardsSeq(wxActJiugonggeRecord.getActId());
		Integer nextAwardsSeq;
		if(maxAwardsSeq == null){
			nextAwardsSeq = 0;
		}else {
			nextAwardsSeq = maxAwardsSeq+1;
		}
		wxActJiugonggeRecord.setSeq(nextAwardsSeq);
		wxActJiugonggeRecord.setRecieveTime(new Date());
		jiugonggeRecordMapper.insertJiugonggeRecord(wxActJiugonggeRecord);
		 //查询访问人的信息 
		 JiugonggeRegistration wxActJiugonggeRegistration =  jiugonggeRegistrationMapper.queryRegistrationByOpenidAndActIdAndJwid(wxActJiugonggeRecord.getOpenid(), wxActJiugonggeRecord.getActId(), wxActJiugonggeRecord.getJwid());
		 wxActJiugonggeRegistration.setAwardsStatus("1");
		 wxActJiugonggeRegistration.setAwardsNum(wxActJiugonggeRegistration.getAwardsNum()+1);//抽奖次数+1
		 jiugonggeRegistrationMapper.updateJiugonggeRegistration(wxActJiugonggeRegistration);//重置状态	
		 if(StringUtils.isEmpty(wxActJiugonggeRecord.getAwardsId())){
			 return new JiugonggePrizes();			 
		 }else{
			 jiugonggeRelationMapper.updateRemainNum(wxActJiugonggeRecord.getActId(), wxActJiugonggeRecord.getJwid(), wxActJiugonggeRecord.getAwardsId());
			 return jiugonggePrizesMapper.queryByAwardIdAndActId(wxActJiugonggeRecord.getAwardsId(),wxActJiugonggeRecord.getActId()).get(0);
		 }
	}


	
}
